
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xim/common/get_style_view.dart';
import 'package:xim/common/util/login.dart';
import 'package:xim/common/util/toast.dart';
import 'package:xim/config/route_config.dart';
import 'package:xim/config/store_config.dart';
import 'package:xim/screen/login_screen/welcome_login_page/welcome_login_controller.dart';
import 'package:xim/screen/login_screen/welcome_login_page/welcome_login_style.dart';

/// 登录欢迎页
class WelcomeLoginPage extends GetStyleControllerView<bool, WelcomeLoginController, WelcomeLoginStyle> {
  WelcomeLoginPage({super.key});

  final _formKey = GlobalKey<FormState>();
  final waringColor = const Color(0xffd63847);

  @override
  Widget buildView(BuildContext context, bool? data) {
    return GestureDetector(
      onTap: controller.missFocus,
      child: Scaffold(
        appBar: style.appBar,
        backgroundColor: style.bgColor.color,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(flex: 1,child: Container(),),
            Center(child: style.title,),
            const SizedBox(height: 30,),
            Expanded(
              flex: 3,
              child: Form(
                key: _formKey,
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 14.0),
                  child: Column(
                    children: [
                      style.passwordInput(controller.focusNode, (val){
                        controller.updatePassword(val??'');
                      },() async {
                        // 指纹调用
                        var res = await LoginUtil.fingerprintValid();
                        if(res){
                          Routers.toHome(type: RouteType.replaceAll);
                        }else{
                          // 指纹登录失败
                          if(context.mounted){
                            ToastUtil.snackbar(title: "登录失败",content: "指纹校验错误", context: context);
                          }
                        }
                      }),
                      const SizedBox(height: 64,),
                      GestureDetector(
                          child: style.loginBtn,
                          onTap: () async {
                            // valid password
                            var res = await LoginUtil.passwordValid(controller.password.value);
                            if(!res){
                              // 密码错误
                              if(context.mounted){
                                ToastUtil.snackbar(title: "登录失败",content: "密码错误", context: context);
                              }else{
                                Routers.toHome(type: RouteType.replaceAll);
                              }
                            }
                          },
                      ),
                      const SizedBox(height: 20,),
                      GestureDetector(
                        onTap: ()=>showWaringDialog(),
                        child: style.hintText,
                      ),
                      const SizedBox(height: 24,),
                    ],
                  ),
                ),
              )
            )
          ],
        ),
      ),
    );
  }

  showWaringDialog(){
    ToastUtil.waringDialog(
        "你确定要重置该账号吗?",
        bgColor: style.waringBgColor.color,
        titleStyle: TextStyle(
            fontSize: 20,
            color: waringColor
        ),
        icon: Icon(Icons.warning, size: 54,color: waringColor,),
        customContent: Column(
          children: [
            Text(
              "目前该账号所有数据将从这个程序中移除，此操作无法撤销?",
              style: TextStyle(
                  color: style.textColor.color,
                  fontSize: 16
              ),
            ),
            const SizedBox(height: 18,),
            Text(
              "本应用将移除您的账号消息，只能通过助记符或私钥恢复该应用",
              style: TextStyle(
                  color: style.textColor.color,
                  fontSize: 14
              ),
            ),
          ],
        ),
      actions: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: () async {
                // clear user data
                await StoreApi.instance.clearUser();
                Routers.toLoginGuidePage(type: RouteType.replaceAll);
              },
              child: Container(
                width: 0.4.sw,
                padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                decoration: BoxDecoration(
                  color: waringColor,
                  borderRadius: const BorderRadius.all(Radius.circular(50)),
                ),
                child: Text(
                  "我明白，继续",
                  style: TextStyle(color: style.textColor.color),
                ),
              ),
            ),
            const SizedBox(height: 10,),
            GestureDetector(
              onTap: (){
                // EasyLoading.show(status: 'loading...');
                Routers.back();
              },
              child: Container(
                width: 0.4.sw,
                padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                decoration: BoxDecoration(
                  border: Border.all(color: style.waringBgColor.reverse),
                  borderRadius: const BorderRadius.all(Radius.circular(50),),
                  color: Colors.transparent
                ),
                child: Center(
                    child: Text(
                        "取消",
                        style: TextStyle(color: style.textColor.color)
                    )
                ),
              ),
            )
          ],
        )
      ]
    );
  }

}
